package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.system.domain.GxDeptStatement;
import com.ruoyi.system.domain.GxUserStatement;
import com.ruoyi.system.mapper.GxDeptStatementMapper;
import com.ruoyi.system.mapper.GxUserStatementMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.GxTempStatementMapper;
import com.ruoyi.system.domain.GxTempStatement;
import com.ruoyi.system.service.IGxTempStatementService;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

/**
 * 模板语句Service业务层处理
 * 
 * @author lsn
 * @date 2023-01-14
 */
@Service
public class GxTempStatementServiceImpl implements IGxTempStatementService 
{
    @Autowired
    private GxTempStatementMapper gxTempStatementMapper;

    @Autowired
    private GxUserStatementMapper gxUserStatementMapper;

    @Autowired
    private GxDeptStatementMapper gxDeptStatementMapper;

    /**
     * 查询模板语句
     * 
     * @param tempId 模板语句主键
     * @return 模板语句
     */
    @Override
    public GxTempStatement selectGxTempStatementByTempId(Long tempId)
    {
        return gxTempStatementMapper.selectGxTempStatementByTempId(tempId);
    }

    /**
     * 查询模板语句列表
     * 
     * @param gxTempStatement 模板语句
     * @return 模板语句
     */
    @Override
    public List<GxTempStatement> selectGxTempStatementList(GxTempStatement gxTempStatement)
    {
        return gxTempStatementMapper.selectGxTempStatementList(gxTempStatement);
    }

    @Override
    public List<GxTempStatement> selectGxTempStatementListByUser(GxTempStatement gxTempStatement)
    {
        return gxTempStatementMapper.selectGxTempStatementListByUser(gxTempStatement);
    }

    @Override
    public List<GxTempStatement> selectGxTempStatementListByDept(GxTempStatement gxTempStatement)
    {
        return gxTempStatementMapper.selectGxTempStatementListByDept(gxTempStatement);
    }

    /**
     * 新增模板语句
     * 
     * @param gxTempStatement 模板语句
     * @return 结果
     */
    @Override
    public int insertGxTempStatement(GxTempStatement gxTempStatement)
    {
        int rows = gxTempStatementMapper.insertGxTempStatement(gxTempStatement);
        Long userId = gxTempStatement.getUserId();
        Long deptId = gxTempStatement.getDeptId();
        if (userId == null && deptId == null) {
            return 0;
        }
        // 医生新增语句模板
        if (userId != null && deptId == null) {
            insertUserStatement(gxTempStatement);
        }
        // 团队新增语句模板
        if (userId == null && deptId != null) {
            insertDeptStatement(gxTempStatement);
        }
        return rows;
    }

    public void insertUserStatement(GxTempStatement gxTempStatement) {
        Long userId = gxTempStatement.getUserId();
        Long tempId = gxTempStatement.getTempId();
        GxUserStatement userStatement = new GxUserStatement();
        userStatement.setUserId(userId);
        userStatement.setTempId(tempId);
        gxUserStatementMapper.insertUserStatement(userStatement);
    }

    public void insertDeptStatement(GxTempStatement gxTempStatement) {
        Long deptId = gxTempStatement.getDeptId();
        Long tempId = gxTempStatement.getTempId();
        GxDeptStatement deptStatement = new GxDeptStatement();
        deptStatement.setDeptId(deptId);
        deptStatement.setTempId(tempId);
        gxDeptStatementMapper.insertDeptStatement(deptStatement);
    }

    /**
     * 修改模板语句
     * 
     * @param gxTempStatement 模板语句
     * @return 结果
     */
    @Override
    public int updateGxTempStatement(GxTempStatement gxTempStatement)
    {
        return gxTempStatementMapper.updateGxTempStatement(gxTempStatement);
    }

    /**
     * 批量删除模板语句
     * 
     * @param tempIds 需要删除的模板语句主键
     * @return 结果
     */
    @Override
    public int deleteGxTempStatementByTempIds(Long[] tempIds)
    {
        int rows = gxTempStatementMapper.deleteGxTempStatementByTempIds(tempIds);
        for (Long tempId : tempIds) {
            gxUserStatementMapper.deleteUserStatementByTempId(tempId);
            gxDeptStatementMapper.deleteDeptStatementByTempId(tempId);
        }
        return rows;
    }

    /**
     * 删除模板语句信息
     * 
     * @param tempId 模板语句主键
     * @return 结果
     */
    @Override
    public int deleteGxTempStatementByTempId(Long tempId)
    {
        int rows = gxTempStatementMapper.deleteGxTempStatementByTempId(tempId);
        gxUserStatementMapper.deleteUserStatementByTempId(tempId);
        gxDeptStatementMapper.deleteDeptStatementByTempId(tempId);
        return rows;
    }
}
